package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.domain.CourseMarket;
import cn.itsource.ymcc.domain.CourseUserLearn;
import cn.itsource.ymcc.dto.PayResultDto;
import cn.itsource.ymcc.dto.UserAndCourseDto;
import cn.itsource.ymcc.mapper.CourseUserLearnMapper;
import cn.itsource.ymcc.service.ICourseMarketService;
import cn.itsource.ymcc.service.ICourseUserLearnService;
import cn.itsource.ymcc.util.AssertUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import net.sf.jsqlparser.expression.LongValue;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.html.parser.Entity;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Eric Chen
 * @since 2022-07-11
 */
@Service
public class CourseUserLearnServiceImpl extends ServiceImpl<CourseUserLearnMapper, CourseUserLearn> implements ICourseUserLearnService {
    @Autowired
    private ICourseMarketService courseMarketService;
    @Override
    public CourseUserLearn getAbleMovie(Long courseId, Long loginId) {
        EntityWrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("login_id",loginId);
        wrapper.eq("state",0);
        CourseUserLearn courseUserLearn = selectOne(wrapper);
        return courseUserLearn;
    }

    @Override
    public void save(PayResultDto payResultDto) {
        String orderNo = payResultDto.getOrderNo();
        String extParams = payResultDto.getExtParams();
        AssertUtil.isNotNull(extParams,"课程为空");
        UserAndCourseDto userAndCourseDto = JSON.parseObject(extParams, UserAndCourseDto.class);
        Long loginId = userAndCourseDto.getLoginId();
        String courseId = userAndCourseDto.getCourseId();
        String[] split = courseId.split(",");
        Date now = new Date();
        for (String s : split) {
            //查询是否存在
            Long c = Long.valueOf(s);
            EntityWrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
            wrapper.eq("course_id", c);
            wrapper.eq("login_id",loginId);
            CourseUserLearn courseUserLearn = selectOne(wrapper);

            CourseMarket courseMarket = courseMarketService.selectById(c);
            if (courseUserLearn != null){
                //累加时间
                Date date = DateUtils.addDays(courseUserLearn.getEndTime(), courseMarket.getValidDays());
                courseUserLearn.setEndTime(date);
                courseUserLearn.setCourseOrderNo(payResultDto.getOrderNo());
                updateById(courseUserLearn);
            }else {
                courseUserLearn = new CourseUserLearn();
                //当前时间加天数
                Date date = DateUtils.addDays(now, courseMarket.getValidDays());
                courseUserLearn.setEndTime(date);
                courseUserLearn.setCourseId(c);
                courseUserLearn.setCourseOrderNo(payResultDto.getOrderNo());
                courseUserLearn.setState(0);
                courseUserLearn.setCreateTime(now);
                courseUserLearn.setLoginId(loginId);
                courseUserLearn.setStartTime(now);
                insert(courseUserLearn);
            }
        }

    }


}
